import {defineStore} from 'pinia'
import {computed, ref} from 'vue'

const useCounterStore = defineStore('counter', () => {
    //响应式数据
    const count = ref(0)
    const age = ref(18)

    const loading = ref(false)
    //
    // //实现状态管理持久化  localStorage 5mb
    // if (localStorage.getItem("count")) {
    //     // count.value = localStorage.getItem("count")
    //     count.value = JSON.parse(localStorage.getItem("count"))
    // }
    //
    // watch(count, newValue => {
    //     //localStorage.setItem("count", newValue)
    //     localStorage.setItem("count", JSON.stringify(newValue))
    // })

    //计算属性
    const doublecount = computed(() => count.value * 2)

    //方法
    function increment() {
        count.value++
    }

    return {count, increment, doublecount, loading, age}

}, {
    persist: {storage: sessionStorage}  //sessionStorage
    //persist: {storage: localStorage} //默认使用的localStorage 5MB
    // persist: false
})

export default useCounterStore
